/**
 * Created by chenyong on 2015/1/29.
 * 全景图操作公共函数
 */
function pano()
{
    return document.getElementById("panoSWFObject");
}

//获取视图的位置坐标
function pano_get_viewposition(){

    var krpano = document.getElementById("panoSWFObject");
    var fov = Number( krpano.get("view.fov") );
    var v = Number( krpano.get("view.vlookat") );
    var h   = Number( krpano.get("view.hlookat") );
    h = h%360; // restrict to +-360
    if (h<=-180){
        h = 360+h;
    }
    else if(h>180){
        h = 360-h;
    }

    pos = {'fov':fov,'ath':h,'atv':v};
    return pos;

}
//获取url中的参数
//function getUrlParam(name) {
//    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
//    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
//    if (r != null) return unescape(r[2]); return null; //返回参数值
//}
//$(document).ready(function(){
//    var newatv=getUrlParam('atv');
//    var newath=getUrlParam('ath');
//    var newfov=getUrlParam('fov');
//    window.location.href="http://192.168.1.22:3000/pano/sh-waitan/?atv="+newatv+"&ath="+newath+"&fov="+newfov;
//})

function updateUrlWithViewPosition(){

    var hr = window.location.href.split('?');
    var path = hr[0];
    var params = _queryParamsFromPosition(pano_get_viewposition());
    var url = path + params;
    //console.log("qq="+hr[0]);
    //console.log("ww"+params);
    window.history.replaceState({},"ChingKun Big Pixel Panorama" ,url);
    // tell the outer frame (if we are embedded)
    top.postMessage({updateQueryParams: params}, "*");

}
function _queryParamsFromPosition(pos){
    return '?atv='+pos.atv+'&ath='+pos.ath+'&fov='+pos.fov;
}

var prev_click = 0;
var dblclick_timeout = 500;
function pano_dblclick(func)
{
    date = new Date();
    now_click = date.getTime();

    if(now_click < (prev_click+dblclick_timeout)){
        prev_click = 0;
        eval(func+'()');
    }else{
        prev_click = now_click;
    }
}

function pano_screentosphere(coords)
{
    var panoobj = pano();

    if (panoobj && panoobj.get && coords)
    {
        hvs = panoobj.get("screentosphere("+coords.x +","+coords.y +")");
        hva = hvs.split(",");

        return {'ath': Number( hva[0] ), 'atv': Number( hva[1] )};
    }
    return false;
}